package com.hanshushi_jiekou;

import java.util.function.Consumer;

public class ConsumerDemo {
    public static void main(String[] args) {
        operatorString("around1", (String s) -> {
            System.out.println(s);
        });

        operatorString("around2", s -> System.out.println(s));

        operatorString("around3", System.out::println);

        operatorString("around4", s -> System.out.println(new StringBuilder(s).reverse().toString()));
        System.out.println("-------------------------------------");

        operatorString("around5", System.out::println, s -> System.out.println(new StringBuilder(s).reverse().toString()));
        System.out.println("-------------------------------------");

        String[] arr = {"a,20", "b,30", "c,23"};
        printInfo(arr, s -> {
            String name = s.split(",")[0];
            System.out.print("姓名，" + name);
        }, s -> {
            String age = s.split(",")[1];
            System.out.println(",年龄：" + age);
        });


    }

    // 定义一个方法，用不同的方式消费同一个字符串两次
    private static void operatorString(String name, Consumer<String> con1, Consumer<String> con2) {
        /*
        con1.accept(name);
        con2.accept(name);
        */
        // 等价于
        con1.andThen(con2).accept(name);
    }

    // 定义一个方法，消费一个字符串数据
    private static void operatorString(String name, Consumer<String> con) {
        con.accept(name);
    }

    //
    private static void printInfo(String[] strArray, Consumer<String> con1, Consumer<String> con2) {
        for (String array : strArray) {
            con1.andThen(con2).accept(array);
        }
    }
}
